package com.crm.newdao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import com.crm.entity.Business;
import com.crm.entity.Clientdet;

public class ClientdetSupportDao extends SimpleJdbcDaoSupport {

	private static final String SQL_INSERT_Clientdet = "INSERT INTO CRMDB.CLIENTDET( BUSINESS_ID, ORGANISATION, SHRTNAME, WEBSITE, ADDR, DIVISION, TELL, FAX, EMAIL, ORGTYPE, IS_DELETED) VALUES ( ?,?,?,?,?,?,?,?,?,?,? )";

	private static final String SQL_UPDATE_Clientdet = "update Clientdet set BUSINESS_ID = ?, ORGANISATION = ?, SHRTNAME = ?,  WEBSITE = ?, ADDR = ?,  DIVISION = ?, TELL = ?,  FAX = ?, EMAIL = ?, ORGTYPE = ?,  IS_DELETED = ? "
			+ "where id = ?";

	private static final String SQL_SELECT_Clientdet = ""
			+ "select id, BUSINESS_ID, ORGANISATION, SHRTNAME, WEBSITE, ADDR, DIVISION, TELL, FAX, EMAIL, ORGTYPE, IS_DELETED from Clientdet ";

	private static final String SQL_SELECT_Clientdet_BY_ID = SQL_SELECT_Clientdet
			+ " where id=?";

	// <start id="java_getClientdetById" />
	public Clientdet getClientdetById(long id) {
		return null;
		/*return getSimpleJdbcTemplate().queryForObject(
				SQL_SELECT_Clientdet_BY_ID,
				new ParameterizedRowMapper<Clientdet>() {
					public Clientdet mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Clientdet Clientdet = new Clientdet();
						Clientdet.setId(rs.getInt("id"));
						Business b = new Business();
						b.setBusinessId(rs.getInt("BUSINESS_ID"));
						Clientdet.setBusinessId(b);
						Clientdet.setOrganisation(rs.getString("ORGANISATION"));
						Clientdet.setShrtname(rs.getString("SHRTNAME"));
						Clientdet.setWebsite(rs.getString("WEBSITE"));
						Clientdet.setAddr(rs.getString("ADDR"));
						Clientdet.setDivision(rs.getString("DIVISION"));
						Clientdet.setTell(rs.getString("TELL"));
						Clientdet.setFax(rs.getString("FAX"));
						Clientdet.setEmail(rs.getString("EMAIL"));
						Clientdet.setOrgtype(rs.getString("ORGTYPE"));
						Clientdet.setIs_deleted(rs.getBoolean("IS_DELETED"));
						return Clientdet;
					}
				}, id);*/
	}

	// <end id="java_getClientdetById" />

	// <start id="java_addClientdet" />
	public void addClientdet(Clientdet cd) {
		getSimpleJdbcTemplate().update(SQL_INSERT_Clientdet,
				cd.getBusinessId().getBusinessId(), cd.getOrganisation(),
				cd.getShrtname(), cd.getWebsite(), cd.getAddr(),
				cd.getDivision(), cd.getTell(), cd.getFax(), cd.getEmail(),
				cd.getOrgtype(), cd.getIs_deleted());

	}

	// <end id="java_addClientdet" />

	// <start id="java_updateClientdet" />
	public void updateClientdet(Clientdet cd) {
		getSimpleJdbcTemplate().update(SQL_UPDATE_Clientdet,
				cd.getBusinessId().getBusinessId(), cd.getOrganisation(),
				cd.getShrtname(), cd.getWebsite(), cd.getAddr(),
				cd.getDivision(), cd.getTell(), cd.getFax(), cd.getEmail(),
				cd.getOrgtype(), cd.getIs_deleted(), cd.getId());

	}

	// <end id="java_updateClientdet" />

	private int queryForIdentity() {
		return getSimpleJdbcTemplate().queryForInt("call identity()");
	}

	public List<Clientdet> findAllClientdets() {
		List<Clientdet> cds = this.getJdbcTemplate().query(SQL_SELECT_Clientdet,
				new ParameterizedRowMapper<Clientdet>() {
					public Clientdet mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Clientdet cd = new Clientdet();
						cd.setId(rs.getInt("id"));
						Business b = new Business();
						b.setBusinessId(rs.getInt("BUSINESS_ID"));
						cd.setBusinessId(b);
						cd.setOrganisation(rs.getString("ORGANISATION"));
						cd.setShrtname(rs.getString("SHRTNAME"));
						cd.setWebsite(rs.getString("WEBSITE"));
						cd.setAddr(rs.getString("ADDR"));
						cd.setDivision(rs.getString("DIVISION"));
						cd.setTell(rs.getString("TELL"));
						cd.setFax(rs.getString("FAX"));
						cd.setEmail(rs.getString("EMAIL"));
						cd.setOrgtype(rs.getString("ORGTYPE"));
						cd.setIs_deleted(rs.getBoolean("IS_DELETED"));
						return cd;
					}
				});
		return cds;
	}

}
